package com.yanqu.road.dao.impl.activity.rankunionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarDivisionConfig;
import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarFloorConfig;
import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarTimeConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RankUnionWarConfigDaoImpl extends TempDao {

    public static String get1Sql = "select * from t_s_activity_peakunion_time where activityId= ?";
    public Map<Integer, RankUnionWarTimeConfig> getTimeConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, RankUnionWarTimeConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(get1Sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RankUnionWarTimeConfig data = new RankUnionWarTimeConfig();
                data.setTimeId(rs.getInt("timeId"));
                data.setSession(rs.getInt("session"));
                data.setBeginTime(rs.getInt("beginTime"));
                data.setDurTime(rs.getInt("durTime"));
                result.put(data.getSession(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String get2Sql = "select * from t_s_activity_peakunion_division where activityId= ?";
    public Map<Integer, RankUnionWarDivisionConfig> getDivisionConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, RankUnionWarDivisionConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(get2Sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RankUnionWarDivisionConfig data = new RankUnionWarDivisionConfig();
                data.setDivision(rs.getInt("division"));
                data.setScore(rs.getString("score"));
                List<Integer> winScoreList = new ArrayList<>();
                List<Integer> loseScoreList = new ArrayList<>();
                List<String> scoreStrList = StringUtils.stringToStringList(data.getScore(),";");
                for(String scoreStr : scoreStrList){
                    List<Integer> scoreList = StringUtils.stringToIntegerList(scoreStr,"\\|");
                    winScoreList.add(scoreList.get(0));
                    loseScoreList.add(scoreList.get(1));
                }
                data.setWinScoreList(winScoreList);
                data.setLoseScoreList(loseScoreList);
                data.setRewardsPresident(rs.getString("rewardsPresident"));
                data.setRewardsMember(rs.getString("rewardsMember"));
                result.put(data.getDivision(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String get3Sql = "select * from t_s_activity_peakunion_floor where activityId= ?";
    public Map<Integer, RankUnionWarFloorConfig> getFloorConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, RankUnionWarFloorConfig> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(get3Sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                RankUnionWarFloorConfig data = new RankUnionWarFloorConfig();
                data.setFloor(rs.getInt("floor"));
                data.setSpecialRewardAll(rs.getString("specialReward"));
                List<String> specialRewardsList = new ArrayList<>();
                List<Integer> specialRewardsWeightList = new ArrayList<>();
                List<String> tempSpecialRewardList = StringUtils.stringToStringList(data.getSpecialRewardAll(),"\\|");
                data.setSpecialReward(tempSpecialRewardList.get(0));
                if(tempSpecialRewardList.size() > 1){
                    List<String> tempSpecialRewardWeightList = StringUtils.stringToStringList(tempSpecialRewardList.get(1),",");
                    for(String tempSpecialRewardWeight: tempSpecialRewardWeightList){
                        List<String> tempSpecialRewardAndWeightList = StringUtils.stringToStringList(tempSpecialRewardWeight,";");
                        specialRewardsList.add(tempSpecialRewardAndWeightList.get(0));
                        specialRewardsWeightList.add(Integer.valueOf(tempSpecialRewardAndWeightList.get(1)));
                    }
                    data.setSpecialRewardsList(specialRewardsList);
                    data.setSpecialRewardsWeightList(specialRewardsWeightList);
                }
                List<String> rewardsList = new ArrayList<>();
                List<Integer> rewardsWeightList = new ArrayList<>();
                data.setRewards(rs.getString("rewards"));
                List<String> tempRewardsList = StringUtils.stringToStringList(data.getRewards(),"\\|");
                for(String tempRewardsWeight : tempRewardsList){
                    List<String> tempRewardsWeightList = StringUtils.stringToStringList(tempRewardsWeight,";");
                    rewardsList.add(tempRewardsWeightList.get(0));
                    rewardsWeightList.add(Integer.valueOf(tempRewardsWeightList.get(1)));
                }
                data.setRewardsList(rewardsList);
                data.setRewardsWeightList(rewardsWeightList);
                data.setConsume(rs.getString("consume"));
                result.put(data.getFloor(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            //throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }
}
